package cn.wjh.business.controller;

import cn.wjh.business.domain.vo.MailVo;
import cn.wjh.business.domain.vo.RegisterVo;
import cn.wjh.business.service.ILoginService;
import cn.wjh.common.constant.Constants;
import cn.wjh.common.core.controller.BaseController;
import cn.wjh.common.core.domain.AjaxResult;
import cn.wjh.common.valid.ValidationGroup;
import cn.wjh.system.service.ISysConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class LoginController extends BaseController {

	@Resource
	private ILoginService loginService;
	@Resource
	private ISysConfigService configService;

	/*邮箱验证码登录*/
	@PostMapping("/mailLogin")
	public AjaxResult mailLogin(@RequestBody @Validated(ValidationGroup.Query.class) MailVo mailVo)
	{
		AjaxResult ajax = AjaxResult.success();
		// 生成令牌
		String token = loginService.mailLogin(mailVo);
		ajax.put(Constants.TOKEN, token);
		return ajax;
	}

	/*邮箱验证码注册*/
	@PostMapping("/mailRegister")
	public AjaxResult mailRegister(@RequestBody @Validated(ValidationGroup.Query.class) RegisterVo registerVo)
	{
		if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
		{
			return error("当前系统没有开启注册功能！");
		}
		AjaxResult ajax = AjaxResult.success();
		// 生成令牌
		String token = loginService.mailRegister(registerVo);
		ajax.put(Constants.TOKEN, token);
		return ajax;
	}
}
